* This code ensures that the events are sufficiently apart. 

* Algorithm:
*- the first event is fixed
* - starting on the second event, repeat this algorithm over the list of episodes:
	* - if the current event is more than `apart' years apart, this event is kept (the tL state variable is set to the current event's year)
	* - if not, the current event is not considered a large devaluation (and tL is set to the year of the previous event)
	* - tL thus records the year of the last firm event. if tL is not updated in a given episode, then the episode is "dropped"

sort ccode largeDevaluation year
bys ccode largeDevaluation: gen episodeB = _n
replace episodeB=. if largeDevaluation==.
qui su episodeB
local max_epi=`r(max)'

*. Ensure events are `1' years apart
gen tL = . // state variable with year of last valid devaluation
replace tL = year if episodeB==1 // give priority to the first event in the sample
gen displaced = .
forval i=2/`max_epi'{
replace tL = year if episodeB==`i' & year-tL[_n-1]>=`1'
replace tL = tL[_n-1] if episodeB==`i' & year-tL[_n-1]<`1'
replace displaced = 1 if episodeB==`i' & year-tL[_n-1]<`1'
}
replace largeDevaluation=. if year!=tL
